﻿//===============================================================================
// Microsoft Innovation Centre - Western Europe
// Copyright (c) 2008 Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Web.Security;

namespace Microsoft.WVB.Server
{
    [ServiceBehavior]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MembershipService : IMembershipService
    {
        #region IMembershipService Members
        [OperationBehavior]
        public bool ChangePassword(string username, string oldPassword, string newPassword)
        {            
            if ((string.IsNullOrEmpty(username)) || (string.IsNullOrEmpty(oldPassword)) || (string.IsNullOrEmpty(newPassword)))
            {
                throw ExceptionFaultBuilder.GetFault("1001", "ArgumentNullException", "ChangePassword");
            }

            MembershipUser user = Membership.GetUser(username, true);

            if (user != null)
                return user.ChangePassword(oldPassword, newPassword);
            else
                return false;
        }

        [OperationBehavior]
        public CreateMembershipUserResponse CreateUser(CreateMembershipUserRequest request)
        {
            System.Web.Security.MembershipCreateStatus status;
            WVBUser responseUser = null;

            if ((string.IsNullOrEmpty(request.Username)) || (string.IsNullOrEmpty(request.Password)) || (string.IsNullOrEmpty(request.Email)))
            {
                throw ExceptionFaultBuilder.GetFault("1001", "ArgumentNullException", "ChangePassword");
            }

            MembershipUser user = Membership.CreateUser(request.Username, request.Password, request.Email, "passowrdQuestion", "passwordAnswer", true, out status);

            if ((status == System.Web.Security.MembershipCreateStatus.Success) && (user != null))
            {
                responseUser = new WVBUser();
                responseUser.Email = user.Email;
                responseUser.UserName = user.UserName;
                responseUser.PPID = user.UserName;
                responseUser.ProviderName = user.ProviderName;
            }
            else
            {
                throw ExceptionFaultBuilder.GetFault("1002", status.ToString(), "CreateUser");
            }

            return new CreateMembershipUserResponse(responseUser, MembershipCreateStatus.Sucess);
        }

        [OperationBehavior]
        public bool DeleteUser(DeleteMembershipUserRequest request)
        {
            if (string.IsNullOrEmpty(request.Username))
            {
                throw ExceptionFaultBuilder.GetFault("1001", "ArgumentNullException", "DeleteUser");
            }

            return Membership.DeleteUser(request.Username, request.DeleteAllRelatedData);
        }

        [OperationBehavior]
        public GetMembershipUserResponse GetUser(GetMembershipUserRequest request)
        {
            WVBUser userResponse = null;

            if (string.IsNullOrEmpty(request.Username))
            {
                throw ExceptionFaultBuilder.GetFault("1001", "ArgumentNullException", "GetUser");
            }

            MembershipUser user = Membership.GetUser(request.Username, true);

            if (user != null)
            {
                userResponse = new WVBUser();
                userResponse.UserName = user.UserName;
                userResponse.PPID = user.UserName;
                userResponse.Email = user.Email;
                userResponse.CreationDate = user.CreationDate;
                userResponse.ProviderName = user.ProviderName;
            }

            return new GetMembershipUserResponse(userResponse);
        }

        [OperationBehavior]
        public void UpdateUser(UpdateMembershipUserRequest request)
        {
            if ((request.User == null) || (string.IsNullOrEmpty(request.User.UserName)) || (String.IsNullOrEmpty(request.User.Email))) 
            {
                throw ExceptionFaultBuilder.GetFault("1001", "ArgumentNullException", "UpdateUser");
            }

            MembershipUser user = Membership.GetUser(request.User.UserName, true);

            if (user != null)
            {
                user.Email = request.User.Email;
            }
            else
            {
                throw ExceptionFaultBuilder.GetFault("1001", "TargetNotFoundException", "UpdateUser");
            }

            Membership.UpdateUser(user);
        }

        [OperationBehavior]
        public bool ValidateUser(ValidateMembershipUserRequest request)
        {
            if ((String.IsNullOrEmpty(request.Username)) || (String.IsNullOrEmpty(request.Password)))
            {
                throw ExceptionFaultBuilder.GetFault("1001", "ArgumentNullException", "ValidateUser");
            }
            return Membership.ValidateUser(request.Username, request.Password);
        }

        #endregion
    }
}
